package com.hy.service.impl;

import com.hy.Vo.ExamineFlowDetailPurchaseApplyVo;
import com.hy.bean.ExamineFlowDetail;
import com.hy.bean.RespBean;
import com.hy.dao.DeptfundsDao;
import com.hy.dao.ExamineFlowDetailDao;
import com.hy.dao.PurchaseApplyDao;
import com.hy.dao.impl.DeptfundsDaoImpl;
import com.hy.dao.impl.ExamineFlowDetailDaoimpl;
import com.hy.dao.impl.PurchaseAppIyDaoImpl;
import com.hy.service.ExamineFlowDetailService;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.DbUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (ExamineFlowDetail)表服务实现类
 *
 * @author makejava
 * @since 2024-04-29 16:36:06
 */
@Service("examineFlowDetailService")
public class ExamineFlowDetailServiceImpl implements ExamineFlowDetailService {

    private ExamineFlowDetailDao examineFlowDetailDao = new ExamineFlowDetailDaoimpl();
    private PurchaseApplyDao purchaseApplyDao = new PurchaseAppIyDaoImpl();
    private DeptfundsDao deptfundsDao = new DeptfundsDaoImpl();

    /**
     * 通过ID查询单条数据
     *
     * @return 实例对象
     * @param id
     */
//    @Override
//    public ExamineFlowDetail queryById(int id) {
//        return this.examineFlowDetailDao.queryById();
//    }

    @Override
    public ExamineFlowDetailPurchaseApplyVo queryExamineById(Integer id) {
        return examineFlowDetailDao.queryById(id);
    }

    @Override
    public List<ExamineFlowDetailPurchaseApplyVo> listAll(Integer status){
        return this.examineFlowDetailDao.listAll(status);
    }
    /**
     * 新增数据
     *
     * @param title 标题
     * @param status 标题
     * @param padId 购买申请id
     * @return 实例对象
     */
    @Override
    public RespBean insert(String title,Integer status,Integer padId) {
        ExamineFlowDetail examineFlowDetail = new ExamineFlowDetail();
        examineFlowDetail.setTitle(title);
        examineFlowDetail.setPurchaseApplyId(padId);
        examineFlowDetail.setStartTime(new Date());
        int count = 0;
        if (status == 1){
            examineFlowDetail.setApplyType("购买申请");
            examineFlowDetail.setExamineLink("购买审批");
            count = examineFlowDetailDao.insert(examineFlowDetail);
        }

        if (count > 0) {
            return new RespBean().success("添加成功");
        }else {
            return new RespBean().error("添加失败");
        }
    }

    /**
     * 修改数据
     *
     * @param examineFlowDetail 实例对象
     * @return 实例对象
     */
    @Override
    public RespBean update(ExamineFlowDetail examineFlowDetail){
        Connection connection = null;
        try {
            connection = DBManager.getConnection();
            connection.setAutoCommit(false);
            // 判断申请金额否是大于等于部门经费
            List<Map<String,Object>> list = examineFlowDetailDao.listDeptIdOrMoney(examineFlowDetail);
            if (list.size() == 0){
                return new RespBean().error("部门经费不足");
            }
            deptfundsDao.updateDeptBalanceMinus(connection, (BigDecimal) list.get(0).get("DEPT_ID"), (BigDecimal) list.get(0).get("MONEY"));

            int update = examineFlowDetailDao.update(connection,examineFlowDetail);
            int i = purchaseApplyDao.updateStatus(connection, examineFlowDetail.getPurchaseApplyId(), examineFlowDetail.getStatus());
            if (update > 0 && i > 0) {
                connection.commit();
                return new RespBean().success("修改成功");
            }
            return new RespBean().error("修改失败");
        } catch (Exception e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 通过主键删除数据
     *
     * @return 是否成功
     * @param id
     */
    @Override
    public boolean deleteById(int id) {
        return this.examineFlowDetailDao.deleteById() > 0;
    }
}
